package com.iOS.controller;

import com.iOS.dto.FileInfoExecution;
import com.iOS.enums.FileInfoEnum;
import com.iOS.service.IFileInfoService;
import com.iOS.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/file", method = {RequestMethod.POST})
public class FileInfoController {
    @Autowired
    private IFileInfoService fileInfoService;

    @RequestMapping(value = "/upload")
    @ResponseBody
    public Map<String, Object> addfile(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String title = HttpServletRequestUtil.getString(request, "file_title");
        String token = HttpServletRequestUtil.getString(request, "token");
         /*获取文件*/
        MultipartResolver MultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartHttpServletRequest multipartHttpServletRequest = MultipartResolver.resolveMultipart(request);
        MultipartFile file = multipartHttpServletRequest.getFile("file");
        if (title != null && token != null && file != null) {
            try {
                FileInfoExecution fe = fileInfoService.add(token, title, file.getInputStream(), file.getOriginalFilename());
                if (fe.getState() == FileInfoEnum.SUCCESS.getState()) {
                    modelMap.put("success", true);
                    modelMap.put("fileInfo", fe.getFileInfo());
                } else {
                    modelMap.put("success", false);
                    modelMap.put("errMsg", fe.getStateInfo());
                }
            } catch (Exception e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", e.getMessage());
            }
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "参数不完整");
        }
        return modelMap;
    }

    @RequestMapping(value = "/del")
    @ResponseBody
    public Map<String, Object> del(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        int id = HttpServletRequestUtil.getInt(request, "id");
        String token = HttpServletRequestUtil.getString(request, "token");
        if (id != -1 && token != null) {
            FileInfoExecution fe = fileInfoService.delete(token, id);
            if (fe.getState() == FileInfoEnum.SUCCESS.getState()) {
                modelMap.put("success", true);
                modelMap.put("fileInfo", fe.getStateInfo());
            } else {
                modelMap.put("success", false);
                modelMap.put("errMsg", fe.getStateInfo());
            }
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "参数不完整");
        }
        return modelMap;
    }

    @RequestMapping(value = "/searchbyid")
    @ResponseBody
    public Map<String, Object> searchById(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        int id = HttpServletRequestUtil.getInt(request, "id");
        if (id != -1) {
            FileInfoExecution fe = fileInfoService.selectbyId(id);
            if (fe.getState() == FileInfoEnum.SUCCESS.getState()) {
                modelMap.put("success", true);
                modelMap.put("fileInfo", fe.getFileInfo());
            } else {
                modelMap.put("success", false);
                modelMap.put("errMsg", fe.getStateInfo());
            }
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "参数不完整");
        }
        return modelMap;
    }

    @RequestMapping(value = "/showbyuser")
    @ResponseBody
    public Map<String, Object> showByUser(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        int userId = HttpServletRequestUtil.getInt(request, "user_id");
        if (userId != -1) {
            FileInfoExecution fe = fileInfoService.selectByUser(userId);
            if (fe.getState() == FileInfoEnum.SUCCESS.getState()) {
                modelMap.put("success", true);
                modelMap.put("userFileInfo", fe.getFileInfoList());
            } else {
                modelMap.put("success", false);
                modelMap.put("errMsg", fe.getStateInfo());
            }
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "参数不完整");
        }
        return modelMap;
    }

    @RequestMapping(value = "/showall")
    @ResponseBody
    public Map<String, Object> showAll(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        FileInfoExecution fe = fileInfoService.selectAll();
        if (fe.getState() == FileInfoEnum.SUCCESS.getState()) {
            modelMap.put("success", true);
            modelMap.put("allFileInfo", fe.getFileInfoList());
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", fe.getStateInfo());
        }
        return modelMap;
    }
}
